package com.singleton.demo05;
/**
 * 序列键生成器 采用单例模式
 */
public class KeyGenerator {
    private static KeyGenerator instance = new KeyGenerator();
    private KeyGenerator(){}
    private int key=1;

    public static KeyGenerator getInstance(){
        return instance;
    }

    public synchronized int getKey(){
        return getKeyFromDB();
    }

    private int getKeyFromDB (){
        String updateSQL = "update keyTable set keyValue = keyValue+1";
        String querySQL = "select keyValue from keyTable";
        // 执行updateSQL
        // 执行querySQL
        // 在此示意性的返回一个key值
        return key++;
    }
}
